package com.shg.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.shg.bean.Ad;
import com.shg.bean.Book;
import com.shg.bean.PageView;
import com.shg.bean.QueryResult;
import com.shg.service.AdService;
import com.shg.web.tools.WebTool;

@Service
public class AdServiceBean extends BasiceDaoImpl<Ad> implements AdService{

	public QueryResult<Ad> getAdList(PageView<Ad> pageView) throws Exception{
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("flag", "asc");
		//where type<>1  查询得到书皇广告
		String where=" shuhgad =?";
		Object[] parameters =new Object[]{1};
		QueryResult<Ad> result= getPagination(Ad.class, pageView.getFirstResult(), pageView.getMaxrecord(),where,parameters, orderby);
		for(Ad ad:result.getResultList()){
			Book book=(Book) sessionFactory.getCurrentSession().get(Book.class, ad.getId());
			ad.setBook(book);
		}
		return result;
	}

	/**
	 * 查询广告页
	 */
	public Map<String, Object> getAdList() throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		String sql="from Ad where flag=200 and shuhgad=1";
		List<Ad> list=sessionFactory.getCurrentSession().createQuery(sql).list();
		map.put("bookMap", list);
		map.put("size", list.size());
		return map;
	}

	
	/**
	 * 查询申请的广告书籍
	 * 1、实现查看该商品是否已经申请过广告
	 */
	public Ad getApply(String bookid) throws Exception{
		Book book = null;
		Ad ad = (Ad) sessionFactory.getCurrentSession().get(Ad.class, bookid);
		if(ad==null){
			book = (Book) sessionFactory.getCurrentSession().get(Book.class, bookid);
			ad=new Ad();
			ad.setId(book.getId());
			ad.setName(book.getBookName());
			ad.setLogo(book.getLogo_300());
		}
		return ad;
	}
	/**
	 * 保存广告
	 */

	public void addShuhgAd(Ad ad) throws Exception {
		Ad a = (Ad) sessionFactory.getCurrentSession().get(Ad.class, ad.getId());
		if(a==null){
			sessionFactory.getCurrentSession().save(ad);
		}else{
			String sql="update Ad set shuhgad=1 ,word='"+ad.getWord()+"' where id='"+ad.getId()+"'";
			sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
		}
	}

	public QueryResult<Ad> getShopAd(PageView<Ad> pageView) throws Exception {
		QueryResult<Ad> result=new QueryResult<Ad>();
		 Object[] parameters=new Object[]{WebTool.getLogin().getId(),1};
		 LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		 orderby.put("flag", "desc");
		//where type<>1  查询得到书皇广告
		String whereSQL="shop.id=? and shuhgad=?";
		result = getPagination(Ad.class, pageView.getFirstResult(), pageView.getMaxrecord(), whereSQL, parameters, orderby);
		return result;
	}

	/**
	 * 审核广告申请
	 */
	public void updateFlag(String[] adids, Integer type) throws Exception {
		String sql="update Ad set flag="+type+" where id in(";
		for(String id:adids){
			sql+="'"+id+"',";
		}
		sql=sql.substring(0, sql.lastIndexOf(","))+")";
		sessionFactory.getCurrentSession().createQuery(sql).executeUpdate();
	}
	/**
	 * 店铺广告查询
	 */
	@SuppressWarnings("unchecked")
	public List<Ad> getShopAd(String shop)throws Exception{
		String sql="from Ad where shop='"+shop+"' and shopad=1";
		List<Ad> list = sessionFactory.getCurrentSession().createQuery(sql).list();
		return list;
	}
	/**
	 * 更新数据库记录为店铺广告
	 */
	public void addShopAd(Ad ad) throws Exception {
		Ad a = (Ad) sessionFactory.getCurrentSession().get(Ad.class, ad.getId());
		if(a==null){
			sessionFactory.getCurrentSession().save(ad);
		}else{
			String sql="update Ad set shopad=1 where id='"+ad.getId()+"'";
			sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
		}
	}
	/**
	 * 店铺广告查询
	 */
	public List<Ad> getShop(String id,Integer size) throws Exception {
		String sql="from Ad where shop='"+id+"' and shopad=1";
		List<Ad> list =null;
		if(size!=0)
			list = sessionFactory.getCurrentSession().createQuery(sql).setMaxResults(size).list();
		else
			list = sessionFactory.getCurrentSession().createQuery(sql).list();
		return list;
	}
	/**
	 * 取消店铺广告
	 */
	public List<Ad> updateType(Ad ad)throws Exception{
		String sql="update Ad set shopad=0 where id='"+ad.getId()+"'";
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
		return getShop(ad.getShop().getId(),0);
	}
}
